<template>
    <div class="box">
        <div>修改信息</div>
        <div class="line"><div class="text">密码：</div><input type="text" v-model="password" maxlength="12"></div>
        <div class="line"><div class="text">用户名：</div><input type="text" v-model="name" maxlength="12"></div>
        <div class="line">
            <div class="text">性别：</div>
            <div>
                <input type="radio" name="sex" @click="sex='male'" :checked="issex===1">男
                <input type="radio" name="sex" @click="sex='female'" :checked="issex===2">女
            </div>
        </div>
        <div class="line"><div class="text">所在城市：</div><input type="text" v-model="livecity" ></div>
        <el-button type="success" round size="small" @click="change">确定</el-button>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
            username: '',
            password: '',
            name: '',
            sex: '',
            livecity: '',
            token: '',
            issex:''
        }
    },
    created(){
        this.token = window.sessionStorage.getItem('token')
        axios(`http://localhost:3000/users?id=${this.token}`).then(res=>{
            this.username = res.data[0].username
            this.password = res.data[0].password
            this.name = res.data[0].name
            this.sex = res.data[0].sex
            this.livecity = res.data[0].livecity
        })
    },
    methods:{
        change(){
            if(this.password.length<6){
                alert('密码不能为空且不能小于6位')
            }else if(this.name.length<1){
                alert('用户名不能小于1')
            }else if(this.sex.length<1){
                alert('请选择性别')
            }else if(this.livecity.length<1){
                alert('所在城市不能位空')
            }else{
                axios({
                url:`http://localhost:3000/users`,
                method:'put',
                data:{
                    username: this.username,
                    name: this.name,
                    password: this.password,
                    sex: this.sex,
                    livecity: this.livecity,
                }
            }).then(res=>{
                this.$emit('changeFinish',res.data)
            })}
        }
    },
    watch:{
        sex(newValue){
            if(newValue === 'male'){
                this.issex = 1
            }else if(newValue === 'female'){
                this.issex = 2
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.box{
    padding: 0 0.45rem;
    text-align: center;
}
.line{
    display: flex;
    margin: 0.05rem 0;
}
.text{
    width: 0.8rem;
}
</style>